<?php
class announce extends base{

  function __construct()
  {
    parent::__construct();
    $this->m = load('m/announce');
  }

  function index()
  {
    $tot = $this->m->count();
    $psize = 30;
    $param['pagination'] = pagination($tot , seg(3) , $psize ,'/announce/index/');
    $param['records'] = $this->m->get("", seg(3), $psize);
    $this->display('announce/list-table',$param);
  }


  function view($id)
  {
    $param['r'] = $this->m->get($id);
    $this->display('announce/show',$param);
  }


  function add()
  {
    $conf = array('title'=>'required','content'=>'required');
    $err = validate($conf);
    if ( $err === TRUE) {
      $elem = array('created'=>time(),'edited'=>time(),
      'title'=>$_POST['title'],
      'content'=>$_POST['content']
      );
    
      $this->m->add($elem);
      redirect('/announce/','发布成功！');
    }
    else {
      $param['val'] = $_POST; 
      $param['err'] = $err;
      $this->display('announce/add',$param);    
    }
  }    

  function edit($id)
  {
    $conf = array('title'=>'required','content'=>'required','created'=>'required','edited'=>'required',);
    $err = validate($conf);
  	if ( $err === TRUE) {
      $this->m->update($id);
      redirect('/announce/','修改成功！');
    }
    else {
      $param['val'] = array_merge($_POST,$this->m->get($id));
      $param['err'] = $err;
      $this->display('announce/add',$param);    
    }
  }

  function del($id)
  {
    $this->m->del($id);
    redirect('/announce/','删除成功！');
  }
}
